using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;

using Framework;

namespace SchemaMonitor
{
	//Collection Class (Customisable half)
	public partial class CNodeLabels_OneToOneList
	{
		#region Filters

		public CNodeLabels_OneToOne GetByType_(int typeId) { var mm = GetByTypeId(typeId); return mm.Count > 0 ? mm[0] : null; }
		public CMetaTag GetByType(int typeId) { return GetByType_(typeId)?.MetaTag; }
		#endregion

		#region Aggregation
		#endregion


		#region Members
		private CNodeList _nodes;
		private List<Guid> _nodeIds;



		private CMetaTagList _metatags;
		private List<int> _metatagIds;
		#endregion

		#region Resolve Associative table (and sort)
		public CNodeList Nodes
		{
			get
			{
				if (_nodes == null)
				{
					lock (this)
					{
						if (null == _nodes)
						{
							_nodes = new CNodeList(this.Count);
							foreach (CNodeLabels_OneToOne i in this)
								_nodes.Add(i.Node());
							_nodes.Sort();
						}
					}
				}
				return _nodes;
			}
		}


		public CMetaTagList Metatags
		{
			get
			{
				if (_metatags == null)
				{
					lock (this)
					{
						if (null == _metatags)
						{
							_metatags = new CMetaTagList(this.Count);
							foreach (CNodeLabels_OneToOne i in this)
									_metatags.Add(i.MetaTag);
							_metatags.Sort();
						}
					}
				}
				return _metatags;
			}
		}
		public CMetaTagList RemainingMetatags(string search)
		{
			CMetaTagList temp = new CMetaTagList(CMetaTag.Cache.Search(search));
			temp.Remove(this.Metatags);
			return temp;
		}
		#endregion

		#region Resolve/Isolate PKs
		public List<Guid> NodeIds
		{
			get
			{
				if (null == _nodeIds)
				{
					lock (this)
					{
						if (null == _nodeIds)
						{
							_nodeIds = new List<Guid>(this.Count);
							foreach (CNodeLabels_OneToOne i in this)
								_nodeIds.Add(i.LabelNodeGuid);
						}
					}
				}
				return _nodeIds;
			}
		}
		public List<int> MetatagIds
		{
			get
			{
				if (null == _metatagIds)
				{
					lock (this)
					{
						if (null == _metatagIds)
						{
							_metatagIds = new List<int>(this.Count);
							foreach (CNodeLabels_OneToOne i in this)
								_metatagIds.Add(i.LabelTagId);
						}
					}
				}
				return _metatagIds;
			}
		}
		#endregion


		#region Cloning
		public CNodeLabels_OneToOneList Clone(CDataSrc target) //, int parentId)
        {
            //No Transaction
            if (target is CDataSrcRemote)
                return Clone(target, null); //, parentId);

            //Transaction
            using (IDbConnection cn = target.Local.Connection())
            {
                IDbTransaction tx = cn.BeginTransaction();
                try
                {
                    CNodeLabels_OneToOneList clone = Clone(target, tx); //, parentId);
                    tx.Commit();
                    return clone;
                }
                catch
                {
                    tx.Rollback();
                    throw;
                }
            }
        }
        public CNodeLabels_OneToOneList Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            CNodeLabels_OneToOneList list = new CNodeLabels_OneToOneList(this.Count);
            foreach (CNodeLabels_OneToOne i in this)
                list.Add(i.Clone(target, txOrNull)); //, parentId));  *Child entities must reference the new parent
            return list;
        }
        #endregion
        
        #region Export to Csv
        //Web - Need to add a project reference to System.Web, or comment out these two methods
        public void ExportToCsv(HttpResponse response) { ExportToCsv(response, "NodeLabels_OneToOnes.csv"); }
        public void ExportToCsv(HttpResponse response, string fileName)
        {
            CDataSrc.ExportToCsv(response, fileName); //Standard response headers
            StreamWriter sw = new StreamWriter(response.OutputStream);
            ExportToCsv(sw);
            sw.Flush();
            response.End();
        }

        //Non-web
        public void ExportToCsv(string filePath)
        {
            StreamWriter sw = new StreamWriter(filePath);
            ExportToCsv(sw);
            sw.Close();
        }

        //Logic
        protected void ExportToCsv(StreamWriter sw)
        {
            string[] headings = new string[] {"LabelNodeGuid", "LabelTypeId", "LabelTagId"};
            CDataSrc.ExportToCsv(headings, sw);
            foreach (CNodeLabels_OneToOne i in this)
            {
                object[] data = new object[] {i.LabelNodeGuid, i.LabelTypeId, i.LabelTagId};
                CDataSrc.ExportToCsv(data, sw);
            }
        }
        #endregion
    }
}
